package com.server.push;

import com.server.dao.AlarmDao;
import com.server.dao.MobileAppAlarmDao;
import com.server.dao.MobileAppDao;
import com.server.libs.login.AppAuth;
import com.server.libs.login.data.AppCache;
import com.server.libs.login.AppOline;
import com.server.pojo.Alarm;
import com.server.pojo.MobileApp;
import com.server.push.dto.resp.AlarmOP;
import com.server.push.dto.resp.AlarmsOP;
import com.server.push.dto.resp.MobileAppsOP;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Configuration
@EnableScheduling    //开启定时任务
@Slf4j
public class Task {

    @Autowired
    AlarmDao alarmDao;

    @Autowired
    AppAuth appAuth;

    @Autowired
    OnlinePush onlinePush;

    @Autowired
    MobileAppAlarmDao mobileAppAlarmDao;

    @Autowired
    MobileAppDao mobileAppDao;

    @Autowired
    AppOline appOline;


    @Scheduled(fixedDelay = 30 * 1000)
    @Transactional(readOnly = true)
    public void mobileApps() {
        List<MobileApp> online = appOline.getMobileApps();
        for (MobileApp mobileApp : online) {
            List<Alarm> alarms = mobileAppAlarmDao.getAlarms(mobileApp, PageRequest.of(0, 100));
            alarms = alarms.stream().filter(var->!var.isExpired()).collect(Collectors.toList());
            Set<MobileApp> mobileApps = new HashSet<>();
            for (Alarm alarm : alarms) {
                mobileApps.addAll(mobileAppAlarmDao.getMobileApps(alarm));
            }


            List<AppCache> all = appOline.getAll();
            mobileApps = mobileApps.stream().filter(var->var.isOnline(all)).collect(Collectors.toSet());
            onlinePush.send(mobileApp, new MobileAppsOP(mobileApps));
        }
    }

    @Scheduled(fixedDelay = 30 * 1000)
    @Transactional
    public void Alarms() {
        List<MobileApp> online = appOline.getMobileApps();
        for (MobileApp mobileApp : online) {
            List<Alarm> alarms = mobileAppAlarmDao.getAlarms(mobileApp, PageRequest.of(0, 100));
            if(alarms != null && !alarms.isEmpty()) {
                alarms = alarms.stream().filter(var->!var.isExpired()).collect(Collectors.toList());
                onlinePush.send(mobileApp, new AlarmsOP(alarms));
                return;
            }
            onlinePush.send(mobileApp, new AlarmsOP(new ArrayList<>()));
        }
    }



    @Scheduled(fixedDelay = 5000) // 每5秒执行一次
    @Transactional
    public void sendAlarmToOnlineUsers() {
        List<Alarm> alarms = alarmDao.needPush();
        for (Alarm alarm : alarms) {
            List<MobileApp> mobileApps = alarm.getNowMobileApps();
            if (mobileApps.isEmpty()) continue;

            log.info("【在线推送用户警报】-"+alarm.getId()+"-"+alarm.getUnit().getName()+"-"+ mobileApps.stream().map(MobileApp::getAccount).collect(Collectors.toList()));

            List<MobileApp> androidUsers = mobileApps.stream()
                    .filter(app -> app.getDeviceType().equalsIgnoreCase("android"))
                    .collect(Collectors.toList());
            onlinePush.send(androidUsers, new AlarmOP(alarm));

            List<MobileApp> iosApps = mobileApps.stream()
                    .filter(app -> app.getDeviceType().equalsIgnoreCase("ios"))
                    .collect(Collectors.toList());
            onlinePush.send(iosApps, new AlarmOP(alarm).setRing(null).setShowNotification(false));
        }
    }

    @Scheduled(fixedDelay = 5000) // 每30秒执行一次
    @Transactional
    public void sendAlarmToOfflineUsers() {
        List<Alarm> alarms = alarmDao.needPush();
        for (Alarm alarm : alarms) {
            List<MobileApp> apps = alarm.getNowMobileApps();
//            if (mobileApps.isEmpty()) continue;
//            List<MobileApp> apps = mobileApps
//                    .stream()
//                    .filter(app -> app.getDeviceType().equalsIgnoreCase("ios"))
//                    .collect(Collectors.toList());

            if (apps.isEmpty()) continue;
            List<String> collect = apps.stream().map(MobileApp::getAccount).collect(Collectors.toList());
            log.info("【阿里推送用户警报】-"+alarm.getId()+"-"+alarm.getUnit().getName()+"-"+ collect);
            AliYunPush.alarm(apps, alarm);
            onlinePush.send(apps, new AlarmOP(alarm).setRing(null).setShowNotification(false));

        }
    }


}
